const table = 'VyB2Kz79QWYjpiD5lRCIMwJEhqFSx0GN1cveZfU4gs6rk8dPbLtAomOnT3';
const ss = [2, 7, 5, 1, 4, 8, 3, 0, 6];
const xor = 177451812;
const add = 8728348608;
const mp2 = new Map();
// 根据企业返回的企业详情url获取插件的域名和参数
export function getPlugData(str: string, spStr: string) {
  const domainArr = str.split(spStr);
  const paramArr = str.indexOf('?');
  const strLength = str.length;
  const domain = domainArr[0];
  const param = str.slice(paramArr, strLength);
  return { domain, param };
}
// 截取字符串中的企业ID，转化加密
export function formatCompanyId(str: string): string {
  const resArr = str.split('?');
  let plugUrl = resArr[0];
  const lastIndex = plugUrl.lastIndexOf('/');
  const totalLength = plugUrl.length;
  const plugAddr = plugUrl.slice(0, lastIndex + 1);
  const plugId = plugUrl.slice(lastIndex + 1, totalLength);
  const eId = encodeStr(plugId);
  plugUrl = plugAddr + eId + '?';
  resArr.forEach((item, index) => {
    if (index) {
      plugUrl += item;
    }
  });
  return plugUrl;
}
// 企业id加密
export function encodeStr(str: string, secondary: boolean): string {
  let content = secondary ? str : '         ';
  let result = '';
  if (!secondary) {
    let s = parseInt(str);
    s = (s ^ xor) + add;
    for (let i = 0; i < table.length; i++) {
      const s1 = table.substring(i, i + 1);
      mp2.set(i, s1);
    }
    for (let i = 0; i < 9; i++) {
      const r = mp2.get(parseInt((s / power(58, i)) % 58));
      content = changeStr(content, ss[i], r);
    }
  }
  //二次加密
  const idArr = content.split('');
  for (let i = 0; i < idArr.length; i++) {
    const hex = idArr[i].charCodeAt().toString(16);
    result = result + hex;
  }
  return result;
}
export function power(a: number, b: number): number {
  let power = 1;
  for (let c = 0; c < b; c++) {
    power *= a;
  }
  return power;
}
export function changeStr(str: string, index: number, changeStr: string): string {
  return str.substr(0, index) + changeStr + str.substr(index + changeStr.length);
}
